package com.iflytek.cuckoo.utils;

import org.springframework.util.DigestUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>是否为空</p>
 *
 * @Author ymhuang3 2017年1月10日
 */
public class StringUtils {
    /**
     * <p>判断Object不为空</p>
     *
     * @param obj
     * @return
     * @Author ymhuang3 2017年1月10日
     */
    public static boolean isNotBlank(Object obj) {
        if (obj != null) {
            String str = obj.toString().trim();
            if (str.length() == 0) {
                return false;
            }
            return true;
        }
        return false;
    }

    /**
     * <p>将String 转为 List</p>
     *
     * @param strVal example "张三,李四,王五"
     * @return [张三, 李四, 王五]
     * @Author ymhuang3 2017年1月25日
     */
    public static List<String> string2List(String strVal) {
        String[] strArray = strVal.split(",");
        List<String> asList = Arrays.asList(strArray);
        return asList;
    }

    /**
     * <p>String 转为 带'号的字符串,用于数据库 in 查询</p>
     *
     * @param strVal example "张三,李四,王五"
     * @return "'张三','李四','王五'"
     * @Author ymhuang3 2017年1月25日
     */
    public static String string2str(String strVal) {
        String[] strArray = strVal.split(",");
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < strArray.length; i++) {
            stringBuffer.append("'" + strArray[i] + "',");
        }
        String strString = stringBuffer.toString();
        String resString = strString.substring(0, strString.length() - 1);
        return resString;
    }

    /**
     * <p>获取map里key值组成String</p>
     *
     * @param params List<Map<String,Object>> <br/>
     *               {"name":"张三","name":"李四","name":"王五"}
     * @param key    name
     * @return "张三,李四,王五"
     * @Author ymhuang3 2017年1月25日
     */
    public static String getString(List<Map<String, Object>> params, String key) {
        StringBuffer stringBuffer = new StringBuffer();
        for (Map<String, Object> map : params) {
            stringBuffer.append(map.get(key) + ",");
        }
        String str = stringBuffer.toString();
        return str.substring(0, str.length() - 1);
    }

    /**
     * <p>List<String> 转为 带'号的字符串,用于数据库 in查询</p>
     *
     * @param ids example [张三,李四,王五]
     * @return "'张三','李四','王五'"
     * @Author ymhuang3 2017年1月25日
     */
    public static String list2Str(List<String> ids) {
        Object[] strArray = ids.toArray();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < strArray.length; i++) {
            stringBuffer.append("'" + strArray[i] + "',");
        }
        String strString = stringBuffer.toString();
        strString = strString.substring(0, strString.length() - 1);
        return strString;
    }

    /**
     * @param fileName 文件名
     * @Describe: 获取文件后缀, 如果没有文件名则返回""
     * @author: ymhuang3 in 2017-11-30
     */
    public static String getSuffix(String fileName) {
        if (fileName.contains(".")) {
            return fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
        }
        return "";
    }

    /**
     * <p>格式化时间字符串
     * <p>将2018-05-02T20:30:02.748+08:00 格式字符串 加工为
     * 2018-05-02 20:30:02
     *
     * @param
     * @return
     * @author zpyin
     * @date 14:47 2018/11/14
     * @see [类、类#方法、类#成员]
     */
    public static String getDate(String date) {
        if (date != null) {
            if (date.contains("T") && date.contains(".")) {
                String[] strArrs = date.split("T");
                StringBuffer stringBuffer = new StringBuffer(strArrs[0]);
                stringBuffer.append(" ");
                stringBuffer.append(strArrs[1].split("\\.")[0]);
                return stringBuffer.toString();
            }
        }
        return "";
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static String getMethodName(String fildeName) {
        byte[] items = fildeName.getBytes();
        items[0] = (byte) ((char) items[0] - 'a' + 'A');
        return new String(items);
    }

    public static String getShortPath(String filepath) {
        String separator = "/";
        if (filepath.endsWith(separator)) {
            filepath = filepath.substring(0, filepath.length() - 1);
            int idx = filepath.lastIndexOf(separator);
            filepath = filepath.substring(idx + 1);
        }
        return filepath;
    }

    public static String getSuffixNum(String str) {
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        String ret = m.replaceAll("").trim();

        return ret;
    }

    public static String getFilePath(String fullPath) {
        if (fullPath.startsWith("/")) {
            fullPath = fullPath.substring(1);
        }

        int idx = fullPath.indexOf("/");
        String repo = fullPath.substring(0, idx);
        String filepath = fullPath.substring(idx);

        idx = filepath.lastIndexOf("/");
        filepath = filepath.substring(0, idx + 1);

        return filepath;
    }


    /**
     * 解码
     *
     * @param source
     * @return
     */
    public static byte[] decode(String source) {
        try {
            sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
            return decoder.decodeBuffer(source);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * <p> 获取路径下的文件后缀名
     * <p> 如果传入的是路径，返回空串
     * @param path 文件路径
     * @return java.lang.String
     * @author nnli5
     * @date 17:18 2019/3/12
     * @see [类、类#方法、类#成员]
     */
    public static String getFileSuffixName(String path){
        if(path==null && path.equals("")){
            return "";
        }
        //验证是否包含“.”
        int one = path.lastIndexOf(".");
        if(one==-1){
            return "";
        }
        String suffixName = "";
        //去掉最后的路径符号“/”
        if(path.endsWith("/")){
            suffixName = path.substring(one,path.length()-1);
        }else{
            suffixName = path.substring(one,path.length());
        }

        return  suffixName;
    }

    /**
     * <p>文件路径判断
     *
     * @param path
     * @return java.lang.String
     * @author ymhuang3
     * @date 10:12 2019/02/15
     * @see [类、类#方法、类#成员]
     */
    public static String disposePath(String path) {
        if (path != null) {
            String start = path.substring(0, 1);
            if (!"/".equals(start)) {
                path = "/" + path;
            }
            String end = path.substring(path.length() - 1);
            if (!"/".equals(end)) {
                path = path + "/";
            }
        }
        return path;
    }

    /**
     * <p>去除路径“/”
     *
     * @param path
     * @return java.lang.String
     * @author ymhuang3
     * @date 10:12 2019/02/15
     * @see [类、类#方法、类#成员]
     */
    public static String ridOfSlash(String path) {
        if (path != null) {
            String start = path.substring(0, 1);
            if ("/".equals(start)) {
                path = path.substring(1);
            }
            String end = path.substring(path.length() - 1);
            if ("/".equals(end)) {
                path = path.substring(0, path.length() - 1);
            }
        }
        return path;
    }


    public static int count(String text, String sub) {
        int count = 0, start = 0;
        while ((start = text.indexOf(sub, start)) >= 0) {
            start += sub.length();
            count++;
        }
        return count;
    }
    
    /**
     * <p> 添加前缀后缀 “/”
     * <p>
     * @param str
     * @return java.lang.String
     * @author nnli5
     * @date 16:22 2019/3/7
     * @see [类、类#方法、类#成员]
     */
    public static String prefixAndSuffix(String str){
        if(!str.startsWith("/")){
            str = "/" + str;
        }
        if(!str.endsWith("/")){
            str += "/";
        }
        return str;
    }

    /**
     * <p> 生成仓库回收站的名称
     * <p>
     * @param sourceName 源仓库名称
     * @param targetName 目标仓库名称
     * @return java.lang.String
     * @author nnli5
     * @date 19:16 2019/3/7
     * @see [类、类#方法、类#成员]
     */
    public static String transDeleteName(String sourceName,String targetName){
        sourceName = ridOfSlash(sourceName);
        targetName = ridOfSlash(targetName);
        String prefix = "";
        if(sourceName.contains("-")){
            prefix = sourceName.split("-")[0];
        }
        targetName = prefix + targetName;
        return targetName;
    }
}
